package lobster.games.studios;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;
import lobster.games.studios.R;

public class ImageAdapter extends BaseAdapter {

	/*int mGalleryItemBackground;
	private Context mContext;
	private final int[] values;*/


	private Context context;
	private final int[] values;

	public ImageAdapter(Context context, int[] values) {
		this.context = context;
		this.values = values;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View gridView;
		final Button button;

		if (convertView == null) {
			gridView = new View(context);

			// get layout from mobile.xml
			gridView = inflater.inflate(R.layout.mobile, null);
		} else {
			gridView = (View) convertView;
		}
		button = (Button) gridView.findViewById(R.id.grid_item_image);
		button.setText("" + values[position]);

		button.setOnTouchListener(new OnTouchListener() {	

			public boolean onTouch(View v, MotionEvent event) {
				String aux = "levels.Level" + button.getText().toString();
				
				Class<?> clazz;
				try {
					clazz = Class.forName(aux);
					Intent intent = new Intent(context, clazz);
					((Activity)context).startActivity(intent);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return false;
			}
		});
		button.setOnClickListener(new OnClickListener() {	

			public void onClick(View v) {
				String aux = "Level" + button.getText().toString();
				Toast.makeText(context, "ola", 5);

				Class<?> clazz;
				try {
					clazz = Class.forName(aux);
					Intent intent = new Intent(context, clazz);
					((Activity)context).startActivity(intent);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		return gridView;
	}

	public int getCount() {
		return values.length;
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}
	

	/*public ImageAdapter(Context c,  int[] values) {
		mContext = c;
		this.values = values;
		mGalleryItemBackground = R.drawable.windowssssicon;
		/*TypedArray a = c.obtainStyledAttributes(R.styleable.HelloGallery);
		mGalleryItemBackground = a.getResourceId(R.drawable.windowssssicon, 0);
		a.recycle();
	}

	public int getCount() {
		//return mImageIds.length;
		return values.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		final Button button = new Button(mContext);
		final int pos = position;

		button.setLayoutParams(new Gallery.LayoutParams(100,100));
		button.setBackgroundResource(mGalleryItemBackground);
		button.setText("" + values[position]);

		button.setOnTouchListener(new OnTouchListener() {	

			public boolean onTouch(View v, MotionEvent event) {
				String aux = "levels.Level" + button.getText().toString();
				Toast.makeText(mContext, "ola" + values[pos], Toast.LENGTH_SHORT).show();

				Class<?> clazz = null;
				if(aux != null){
					try {
						clazz = Class.forName(aux);
					
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Intent intent = new Intent(mContext, clazz);
					((Activity)mContext).startActivity(intent);
				}
				return false;
			}
		});

		return button;
	}*/
}